
#include "mainwindow.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QDragLeaveEvent>
#include <QDropEvent>

#include <QMimeData>

#include <QFontDatabase>

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    fontLabel = new QLabel(cnlabel->font().family() + "(默认)");
    cnlabel = new QLabel("你们这些家伙，就不会写个字体预览工具?");
    enlabel = new QLabel("You guys, you don't write a font preview tool?");

// 顶部
    QHBoxLayout *hFmlayout = new QHBoxLayout();
    hFmlayout->addStretch();
    hFmlayout->addWidget(fontLabel);
    hFmlayout->addStretch();

// 中文
    QHBoxLayout *hcnlayout = new QHBoxLayout();
    hcnlayout->addStretch();
    hcnlayout->addWidget(cnlabel);
    hcnlayout->addStretch();
    
// 英文
    QHBoxLayout *henlayout = new QHBoxLayout();
    henlayout->addStretch();
    henlayout->addWidget(enlabel);
    henlayout->addStretch();
    
// 在窗口上下排列
    QVBoxLayout *vlayout = new QVBoxLayout();
    vlayout->addLayout(hFmlayout);
    vlayout->addStretch();
    vlayout->addLayout(hcnlayout);
    vlayout->addLayout(henlayout);
    vlayout->addStretch();

    setLayout(vlayout);
    setAcceptDrops(true);

    resize(450, 250);
    // #E0E0E0
    // setWindowFlags(Qt::FramelessWindowHint);
    setObjectName("widget"); // 设置对象句,相当于css里的id
    // setStyleSheet("#widget {background-color:rgba(40, 40, 40, 10);}");// 设置id对应元素的背景色
}

MainWindow::~MainWindow()
{
	
}


void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    auto mime = event->mimeData();
    if (mime->hasUrls()) {
        auto urls = mime->urls();
        if (urls.length() > 0) {
            if (urls[0].isLocalFile()) {
                auto path = urls[0].toLocalFile();
                if (path.endsWith("ttf")) {
                    return event->accept();
                }
            }
        }
    }

}

void MainWindow::dragMoveEvent(QDragMoveEvent *event)
{
    
}

void MainWindow::dragLeaveEvent(QDragLeaveEvent *event)
{
    
}

void MainWindow::dropEvent(QDropEvent *event)
{
    auto mime = event->mimeData();
    auto urls = mime->urls();

    auto fontPath = urls[0].toLocalFile();
    if (fontPath.endsWith("ttf")) {
        // auto lfont = cnlabel->font();
        // auto lfontinfo = cnlabel->fontInfo();
        // auto lfontmetrics = cnlabel->fontMetrics();

        int appFontId = QFontDatabase::addApplicationFont(fontPath);
        auto families = QFontDatabase::applicationFontFamilies(appFontId);
        
        // lfont.setFamily(families.at(0));
        
        QFont lfont(families.at(0));
        fontLabel->setText(lfont.family());
        cnlabel->setFont(lfont);
        enlabel->setFont(lfont);
    }
}
